package com.ruoyi.common.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker // 启用WebSocket消息代理
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic"); // 定义消息代理，它将消息从服务器路由到客户端
        registry.setApplicationDestinationPrefixes("/app"); // 定义消息发送的前缀
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //System.out.println("注册websocket");
         registry.addEndpoint("/websocket").withSockJS(); // 注册WebSocket端点，客户端将通过这个端点连接服务器
        // registry.addEndpoint("/websocket")
        //         .setAllowedOriginPatterns("*")  // 使用模式匹配所有来源
        //         .withSockJS();
        //System.out.println("注册websocket成功");
    }
}
